import React from 'react';

//计算剩余时间
const computeTimeLife = (planTime, realTime) => {
  let seconds = Math.round(Math.abs((new Date(planTime) - (realTime ? new Date(realTime).getTime() : Date.now()))) / 1000);//单位：s
  const days = Math.floor(seconds / (24 * 60 * 60));
  seconds = seconds - (days * 24 * 60 * 60);
  const hours = Math.floor(seconds / (60 * 60));
  seconds = seconds - (hours * 60 * 60);
  const minutes = Math.floor(seconds / 60);
  seconds = seconds - minutes * 60;
  return { days, hours, minutes, seconds }
}

//渲染时间
const renderTimeLift = ({ planTime, realTime }) => {
  const { days, hours, minutes } = computeTimeLife(planTime, realTime), 
  timeRender = days ? `${days}天${hours}时${minutes}分` : `${hours}时${minutes}分`;

  if(new Date(planTime).getTime() > (realTime ? new Date(realTime).getTime() : Date.now())){
    return <span>剩余时间：{timeRender}</span>;
  }else{
    return <span>剩余时间：<span className='common-color9'>已超时({timeRender})</span></span>
  }
}

export default React.memo(renderTimeLift, (prev, next)=>{
  return prev.deliveryTime == next.deliveryTime;
})